package io.jft.doll.ui.webview

import android.os.Bundle
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.g.base.ui.BaseActivity
import com.just.library.AgentWeb
import io.jft.doll.R
import io.jft.doll.databinding.ViewFrameBinding
import io.jft.doll.router.RouterPage


/**
 * Created by G on 2017/11/30 0030.
 */
@Route(path = RouterPage.WebViewActivity)
class WebActivity : BaseActivity<ViewFrameBinding>() {
    override var hasToolbar: Boolean = true

    @JvmField
    @Autowired
    var url: String = ""

    private lateinit var webView: AgentWeb
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)

        setContentView(R.layout.view_frame)

        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_toolbar_24dp)
        toolbar.setNavigationOnClickListener { if (!webView.back()) finishAfterTransition() }

        webView = AgentWeb.with(this)
                .setAgentWebParent(contentView.layout, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
                .useDefaultIndicator()
                .defaultProgressBarColor()
                .setReceivedTitleCallback { _, title -> toolbar.title = title; showContentView() }
                .createAgentWeb()
                .ready()
                .go(url)
    }

    override fun onBackPressed() {
        if (!webView.back())
            super.onBackPressed()
    }

    override fun onPause() {
        webView.webLifeCycle.onPause()
        super.onPause()

    }

    override fun onResume() {
        webView.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        webView.webLifeCycle.onDestroy()
        super.onDestroy()
    }
}